package com.hunliji.monitor.web.controller.app;

import com.alibaba.fastjson.JSON;
import com.hunliji.common.response.ResponseData;
import com.hunliji.monitor.web.facade.AlertConfigFacade;
import com.hunliji.monitor.web.model.param.AlertChatBindParam;
import com.hunliji.monitor.web.model.param.AlertConfigSaveParam;
import com.hunliji.monitor.web.model.param.AlertUserBindParam;
import com.hunliji.monitor.web.model.param.BatchSaveUserConfigParam;
import com.hunliji.monitor.web.model.param.GetAlertChatBindQueryParam;
import com.hunliji.monitor.web.model.param.GetAlertUserBindQueryParam;
import com.hunliji.monitor.web.model.param.GetAllAlertConfigsParam;
import com.hunliji.monitor.web.model.param.GetUserConfigQueryParam;
import com.hunliji.monitor.web.model.param.QueryChatGroupQueryParam;
import com.hunliji.monitor.web.model.param.SaveUserConfigParam;
import com.hunliji.monitor.web.model.param.feishu.NotifyParam;
import com.hunliji.monitor.web.model.vo.AlertChatBindVO;
import com.hunliji.monitor.web.model.vo.AlertConfigVO;
import com.hunliji.monitor.web.model.vo.AlertUserBindVO;
import com.hunliji.monitor.web.model.vo.ChatGroupVO;
import com.hunliji.monitor.web.model.vo.NotifyVO;
import com.hunliji.monitor.web.model.vo.UserInfoVO;
import java.util.List;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 告警配置模块
 * File：AlertConfigController.java<br> Title: <br> Description: <br>
 *
 * @author lejin Date: 2022/10/10
 */
@RestController
@RequestMapping(value = "/appApi/alertConfig")
@Slf4j
public class AlertConfigController {

    @Resource
    AlertConfigFacade alertConfigFacade;

    /**
     * 获取用户信息列表
     * @param getAllAlertConfigsParam
     * @return
     */
    @GetMapping("/queryUserInfoList")
    public ResponseData<List<UserInfoVO>> queryUserInfoList(GetUserConfigQueryParam getAllAlertConfigsParam){
        return ResponseData.success(alertConfigFacade.queryUserInfoList(getAllAlertConfigsParam));
    }

    /**
     * 查询告警配置列表
     * @param getAllAlertConfigsParam
     * @return
     */
    @GetMapping("/queryAlertConfigs")
    public ResponseData<List<AlertConfigVO>> queryAlertConfigs(GetAllAlertConfigsParam getAllAlertConfigsParam){
        return ResponseData.success(alertConfigFacade.queryAlertConfigs(getAllAlertConfigsParam));
    }

    /**
     * 获取告警用户绑定表数据
     * @param getAlertUserBindQueryParam
     * @return
     */
    @GetMapping("/queryAlertUserBinds")
    public ResponseData<List<AlertUserBindVO>> queryAlertUserBinds(GetAlertUserBindQueryParam getAlertUserBindQueryParam){
        return ResponseData.success(alertConfigFacade.queryAlertUserBinds(getAlertUserBindQueryParam));
    }

    /**
     * 获取告警群聊绑定表数据
     * @param getAlertChatBindQueryParam
     * @return
     */
    @GetMapping("/queryAlertChatBinds")
    public ResponseData<List<AlertChatBindVO>> queryAlertChatBinds(GetAlertChatBindQueryParam getAlertChatBindQueryParam){
        return ResponseData.success(alertConfigFacade.queryAlertChatBinds(getAlertChatBindQueryParam));
    }

    /**
     * 获取群聊列表
     * @param queryChatGroupQueryParam
     * @return
     */
    @GetMapping("/queryChatGroups")
    public ResponseData<List<ChatGroupVO>> queryChatGroups(
        QueryChatGroupQueryParam queryChatGroupQueryParam){
        return ResponseData.success(alertConfigFacade.queryChatGroups(queryChatGroupQueryParam));
    }

    /**
     * 保存告警配置
     * @param notifyParam
     * @return
     */
    @PostMapping("/saveAlertConfig")
    public ResponseData<Boolean> saveAlertConfig(@Validated @RequestBody AlertConfigSaveParam notifyParam) {
        alertConfigFacade.saveAlertConfig(notifyParam);
        return ResponseData.success(Boolean.TRUE);
    }

    /**
     * 删除告警配置
     * @param id
     * @return
     */
    @GetMapping("deleteConfig")
    public ResponseData<Boolean> deleteConfig(@RequestParam("id") Long id) {
        alertConfigFacade.deleteAlertConfig(id);
        return ResponseData.success(Boolean.TRUE);
    }

    /**
     * 群聊与告警绑定
     * @param alertChatBindParam
     * @return
     */
    @PostMapping("/alertChatBind")
    public ResponseData<Boolean> alertChatBind(@Validated @RequestBody AlertChatBindParam alertChatBindParam) {
        alertConfigFacade.alertChatBind(alertChatBindParam);
        return ResponseData.success(Boolean.TRUE);
    }

    /**
     * 用户与告警绑定
     * @param alertUserBindParam
     * @return
     */
    @PostMapping("/alertUserBind")
    public ResponseData<Boolean> alertUserBind(@Validated @RequestBody AlertUserBindParam alertUserBindParam) {
        alertConfigFacade.alertUserBind(alertUserBindParam);
        return ResponseData.success(Boolean.TRUE);
    }

    /**
     * 解除群与告警绑定
     * @param id
     * @return
     */
    @GetMapping("deleteAlertChatBind")
    public ResponseData<Boolean> deleteAlertChatBind(@RequestParam("id") Long id) {
        alertConfigFacade.deleteAlertChatBind(id);
        return ResponseData.success(Boolean.TRUE);
    }

    /**
     * 解除用户与告警绑定
     * @param id
     * @return
     */
    @GetMapping("deleteAlertUserBind")
    public ResponseData<Boolean> deleteAlertUserBind(@RequestParam("id") Long id) {
        alertConfigFacade.deleteAlertUserBind(id);
        return ResponseData.success(Boolean.TRUE);
    }

    /**
     * 修改/创建用户信息
     * @param saveUserConfigParam
     * @return
     */
    @PostMapping("/saveUserConfig")
    public ResponseData<Boolean> saveUserConfig(@Validated @RequestBody SaveUserConfigParam saveUserConfigParam){
        alertConfigFacade.saveUserConfig(saveUserConfigParam);
        return ResponseData.success(Boolean.TRUE);
    }

    /**
     * 批量创建用户
     * @param batchSaveUserConfigParam
     * @return
     */
    @PostMapping("/batchCreateUser")
    public ResponseData<Boolean> batchCreateUser(@Validated @RequestBody BatchSaveUserConfigParam batchSaveUserConfigParam){
        batchSaveUserConfigParam.getFeishuUserIds().forEach(x -> alertConfigFacade.saveUserConfig(
            SaveUserConfigParam.builder().feishuUserId(x).build()));
        return ResponseData.success(Boolean.TRUE);
    }

}
